var GutaPrice;
var GutaPriceDiscount;
var GutaNotes = "";
var GutaInfo = {
	name: "ГУТА-Страхование",
	price: 0,
	discountPrice: 0,
	rating: 0,
	notesToDisplay: new Array(0),
	notesSpecial: '',
	notes: "",
	realFr: 0,
	frArray: new Array(0),
	countSpecialNotes: 0,
	calc: function () {
		calculateGuta();
	}
};
var GutaunableTocalculate = false;
var GutaNotesIndefinite = "";
var GutaXML = false;

function calculateGuta() {
	GutaPrice = 0;
	if (!GutaXML) $.ajax({
		url: "guta/Guta_cars.xml?" + rand % 1000,
		success: function (response) {
			GutaXML = response;
			GutaCalc(GutaXML);
		}
	})
	else
	GutaCalc(GutaXML);
}

function GutaCalc(response) {
	//	var time_0= new Date().getTime();
	GutaInfo.countSpecialNotes = 0;
	var xml = $(response);
	var xmlCars = xml.find('carList').eq(0);
	var Ki = xml.find('Ki').eq(0);
	GutaInfo.notesSpecial = '';
	var specialNotesTMP = '';
	GutaInfo.notesToDisplay = new Array(0);
	var carXML;
	var baseTariff;
	var ku;
	var kh;
	var kAgeExp;
	var kFr;
	var realFr;
	var kRassrochka;
	var kAlarm;

	//   находим машину в xml
	var xmlMan = xmlCars.find("m>n");
	var foundMan = xmlMan.filter(function () {
		return $(this).text().toUpperCase() == manufacturerName.toUpperCase()
	});
	if (foundMan.length > 0) {
		if (foundMan.parent().find("c>n").filter(function () {
			return $(this).text().toUpperCase() == carName.toUpperCase()
		}).length != 0) carXML = foundMan.parent().find("c>n").filter(function () {
			return $(this).text().toUpperCase() == carName.toUpperCase()
		}).parent();
		else if (foundMan.parent().find("c>n").filter(function () {
			return $(this).text() == "*"
		}).length != 0) carXML = foundMan.parent().find("c>n").filter(function () {
			return $(this).text() == "*"
		}).parent();
		else
		carXML = xmlMan.filter(function () {
			return $(this).text() == "*"
		}).parent().find("c>n").filter(function () {
			return $(this).text() == "*"
		}).parent();
	} else
	carXML = xmlMan.filter(function () {
		return $(this).text() == "*"
	}).parent().find("c>n").filter(function () {
		return $(this).text() == "*"
	}).parent();

	//	коэффициенты базового тарифа
	var carKi = carXML.find(city);
	var defaultTariff = Ki.find("defaultTariff>" + city + '>c');
	defParams = defaultTariff.filter(function () {
		return parseInt($(this).find("p").eq(0).text()) > parseInt(price)
	}).first();
	//	alert('dp='+defParams.text());
	if (defParams.length == 0) defParams = defaultTariff.find("c").filter(function () {
		$(this).find('p').eq(0).text() == "*"
	});
	//	var rus = carXML.find("r").eq(0).text()=="1" ? true:false
	baseTariff = parseFloat(carKi.find('bt').eq(0).text());

	//	alert(xml.find('carList>m>n').filter(function(){return $(this).text()=='DAEWOO'}).parent().text());
	ku = carKi.find('u').eq(0);
	kh = carKi.find('h').eq(0);
	var h2 = 1;
	if (carKi.find('h2').length == 1) h2 = 2;
	if (ku.length == 1) ku = parseFloat(ku.text());
	else
	ku = parseFloat(defParams.find("u").eq(0).text());
	if (kh.length == 1) kh = parseFloat(kh.text());
	else
	kh = parseFloat(defParams.find("h").eq(0).text());

	//	alert('carKi='+carKi.text()+'\nbaseTariff='+baseTariff+'\nku='+ku+'\nkh='+kh);
	//	Оплата в рассрочку
	kRassrochka = $("input:checked").filter("[name='oplata']").eq(0).val();
	switch (kRassrochka) {
	case "0":
		kRassrochka = 1;
		break;
	case "1":
		kRassrochka = Ki.find("oplata>" + city + ">o2").eq(0).text();
		break;
	case "2":
		kRassrochka = Ki.find("oplata>" + city + ">o3").eq(0).text();
		break;
	case "3":
		kRassrochka = Ki.find("oplata>" + city + ">o4").eq(0).text();
		break;
	}
	kRassrochka = parseFloat(kRassrochka);
	var corporate = false;
	if ($('#corporatePerson').eq(0).attr('checked')) corporate = true;

	//	Опыт / стаж
	var kiAgeExp = Ki.find("AgeExp").eq(0);
	var kAgeExpMin = 0;
	if (!multiDrive) {
		if (!rus) {
			var drA, drE;
			for (var i = 0; i < driversListForCalc.length; i++) {
				drA = parseInt(driversListForCalc.eq(i).find('.f-driver-c2').eq(0).find('input').eq(0).val());
				drE = parseInt(driversListForCalc.eq(i).find('.f-driver-c2').eq(0).find('input').eq(1).val());
				if (parseInt(drA) < 20) drAId = '18-19';
				else if (parseInt(drA) < 24) drAId = '20-23';
				else if (parseInt(drA) < 27) drAId = '24-26';
				else if (parseInt(drA) < 30) drAId = '27-29';
				else if (parseInt(drA) < 39) drAId = '30-38';
				else if (parseInt(drA) < 56) drAId = '39-55';
				else
				drAId = 'm55';

				if (parseInt(drE) < 3) drEid = '0-2';
				else if (parseInt(drE) < 7) drEid = '3-6';
				else if (parseInt(drE) < 9) drEid = '7-8';
				else if (parseInt(drE) < 17) drEid = '9-16';
				else
				drEid = 'm16';

				var newaek = kiAgeExp.find(city + ">f>a>v").filter(function () {
					return $(this).text() == drAId
				}).eq(0).parent();
				newaek = newaek.find("e>v").filter(function () {
					return $(this).text() == drEid
				}).eq(0).parent();
				newaek = newaek.find("k").eq(0).text();
				newaek = parseFloat(newaek);
				if (newaek > kAgeExpMin) kAgeExpMin = newaek;
			}
			kAgeExp = kAgeExpMin;
		} else {
			var drA, drE;
			for (var i = 0; i < driversListForCalc.length; i++) {
				drA = parseInt(driversListForCalc.eq(i).find('.f-driver-c2').eq(0).find('input').eq(0).val());
				drE = parseInt(driversListForCalc.eq(i).find('.f-driver-c2').eq(0).find('input').eq(1).val());
				if (parseInt(drA) < 21) drAId = '18-20';
				else if (parseInt(drA) < 26) drAId = '21-25';
				else if (parseInt(drA) < 29) drAId = '26-28';
				else if (parseInt(drA) < 41) drAId = '29-40';
				else if (parseInt(drA) < 66) drAId = '41-65';
				else
				drAId = 'm65';

				if (parseInt(drE) < 1) drEid = '0';
				else if (parseInt(drE) < 3) drEid = '1-2';
				else if (parseInt(drE) < 7) drEid = '3-6';
				else if (parseInt(drE) < 12) drEid = '7-11';
				else if (parseInt(drE) < 15) drEid = '12-14';
				else
				drEid = 'm14';

				var newaek = kiAgeExp.find(city + ">d>a>v").filter(function () {
					return $(this).text() == drAId
				}).eq(0).parent();
				newaek = newaek.find("e>v").filter(function () {
					return $(this).text() == drEid
				}).eq(0).parent();
				newaek = newaek.find("k").eq(0).text();
				newaek = parseFloat(newaek);
				if (newaek > kAgeExpMin) kAgeExpMin = newaek;
			}
			kAgeExp = kAgeExpMin;
		}
	} else {
		kAgeExp = kiAgeExp.find(city + ">" + (rus ? "d" : "f") + ">a>v").filter(function () {
			return $(this).text() == '18-19'
		}).eq(0).parent();
		kAgeExp = kAgeExp.find("e>v").filter(function () {
			return $(this).text() == '0-2'
		}).eq(0).parent();
		kAgeExp = kAgeExp.find("k").eq(0).text();
		kAgeExp = parseFloat(kAgeExp);
	}
	if (corporate) if (kAgeExp > 1) kAgeExp = 1;

	//	Способ возмещения ущерба

	var sto = $("input:checked").filter("[name='sto']").eq(0).val();
	var agel3 = parseInt(carAge.replace("carAge", "")) < 3;
	var xmlSTO = Ki.find("stoa>" + city + ">" + (agel3 ? "al3" : "am2")).eq(0);
	switch (sto) {
	case "0":
		sto = parseFloat(xmlSTO.find("strk").eq(0).text());
		break;
	case "1":
		sto = parseFloat(xmlSTO.find("calc").eq(0).text());
		break;
	case "3":
		sto = parseFloat(xmlSTO.find("strl").eq(0).text());
		break;
	case "4":
		sto = parseFloat(agel3 ? xmlSTO.find("d").eq(0).text() : xmlSTO.find("strl").eq(0).text());
		break;
	}
	//	Сигнализация

	kAlarm = 1;
	var alarmSat = 1;
	var XMLAlarm = Ki.find("alarm>" + city);
	if ($("#advSattellite").attr("checked")) {
		alarmSat = parseFloat(Ki.find("sps>other").eq(0).text());
		if ($("#advEchelon").attr("checked")) {
			if ($("#advEchelonStandart").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Echelon>standart").text()) ? alarmSat : parseFloat(XMLAlarm.find("Echelon>standart").text());
			if ($("#advEchelonProfy").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Echelon>profi").text()) ? alarmSat : parseFloat(XMLAlarm.find("Echelon>profi").text());
			if ($("#advEchelonProfyM").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Echelon>profiM").text()) ? alarmSat : parseFloat(XMLAlarm.find("Echelon>profiM").text());
			if ($("#advEchelonVIP").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Echelon>VIP").text()) ? alarmSat : parseFloat(XMLAlarm.find("Echelon>VIP").text());
			if ($("#advEchelonVIPSuper").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Echelon>VIPSuper").text()) ? alarmSat : parseFloat(XMLAlarm.find("Echelon>VIPSuper").text());
		} else if ($("#advArkan").attr("checked")) {
			if ($("#advArkanCity").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Arkan>city").text()) ? alarmSat : parseFloat(XMLAlarm.find("Arkan>city").text());
			if ($("#advArkanSatellite").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Arkan>satellite").text()) ? alarmSat : parseFloat(XMLAlarm.find("Arkan>city").text());
			if ($("#advArkanControl").attr("checked")) alarmSat = alarmSat < parseFloat(XMLAlarm.find("Arkan>control").text()) ? alarmSat : parseFloat(XMLAlarm.find("Arkan>control").text());
		}
	}
	var alarmImm = 1;
	if ($("#advImmobilizer").attr("checked")) {
		if ($("#advTecnoblock").attr("checked") && ($("#advTecnoblock12KS").attr("checked") || $("#advTecnoblockSat").attr("checked"))) {
			alarmImm = parseFloat(XMLAlarm.find("Tecnoblock>strahovoy").eq(0).text());
		} else if ($("#advGreatGuard").attr("checked")) {
			if ($("#advGreatGuardOther").attr("checked")) alarmImm = parseFloat(XMLAlarm.find("GreatGuard>standart").eq(0).text());
			if ($("#advGreatInsurance").attr("checked")) alarmImm = parseFloat(XMLAlarm.find("GreatGuard>strahovoy").eq(0).text());
			if ($("#advGreatGuardSpecial").attr("checked")) alarmImm = parseFloat(XMLAlarm.find("GreatGuard>special").eq(0).text());
			if ($("#advGreatGuardSobr").attr("checked")) alarmImm = parseFloat(XMLAlarm.find("GreatGuard>greatSobr").eq(0).text());
		} else if ($("#advMED").attr("checked")) {
			alarmImm = parseFloat(XMLAlarm.find("MED>MED2250").eq(0).text());
		}
	}
	var alarmTransponder = 1;
	if ($("#advBlackBugSuper").attr("checked")) alarmTransponder = parseFloat(XMLAlarm.find("BlackBug>blackBugBT84").eq(0).text());
	if (alarmSat <= alarmImm && alarmSat <= alarmTransponder) kAlarm = alarmSat;
	else if (alarmImm <= alarmSat && alarmImm <= alarmTransponder) kAlarm = alarmImm;
	else if (alarmTransponder <= alarmSat && alarmTransponder <= alarmImm) kAlarm = alarmTransponder;

	//	Франшиза

	var maxFr = selectedFr;
	var fr;
	var currFr;
	GutaInfo.realFr = 0;
	realFr = 0;
	//	alert("max fr="+maxFr);
	var frTMP;
	var frSearchString = "fr>" + city + (rus ? ">d" : ">f");
	frTMP = Ki.find(frSearchString).eq(0);
	fr = frTMP;
	fr = fr.find("c>p").filter(function () {
		return parseInt($(this).text()) > parseInt(price)
	}).eq(0);
	if (fr.length == 0) fr = frTMP.find("c>p").filter(function () {
		return $(this).text() == "*"
	}).eq(0);
	fr = fr.parent().find('c>v').filter(function () {
		return parseInt($(this).text()) <= parseInt(selectedFr)
	}).filter(":last");
	realFr = parseInt(fr.text());
	fr = fr.parent().find("k").eq(0).text();
	fr = parseFloat(fr);
	if (isNaN(fr) || fr == 0) {
		fr = 1
		realFr = 0;
	}






	var TU = (baseTariff / parseInt(price) * 100 + ku) * fr * kAgeExp * sto;
	var TH = kh * kAlarm * h2;
	var discount = (100 - parseFloat($('#sk3').text())) / 100;



	GutaInfo.price = parseInt((TU + TH) / 100 * kRassrochka * parseInt(price));
	GutaInfo.discountPrice = parseInt(GutaInfo.price * discount);
	GutaInfo.notes = GutaNotes;
	GutaInfo.rating = 4;
	GutaInfo.realFr = realFr;
/*
alert(
'disc='+discount+
'\nTU='+TU+
'\nTH='+TH+
"\nprice="+price+"\n"
+"baseTariff="+baseTariff+"\n"
+"ku="+ku+"\n"
+"kh="+kh+"\n"
+"kAgeExp="+kAgeExp+"\n"
+"kalarm="+kAlarm+"\n"
+"kRassrochka="+kRassrochka+"\n"
+"sto="+sto+"\n"
+"fr="+fr+"\n"
+"GutaInfo.price="+GutaInfo.price+"\n");
*/


	var frArr;
	if (priceChanged) {
		if (rus) {
			if (parseInt(price) <= 180000) frArr = [5000, 7500, 10000, 15000, 20000, 25000];
			else
			frArr = [5000, 7500, 10000, 15000, 20000, 25000, 30000];
		} else {
			if (parseInt(price) <= 400000) frArr = [5000, 7500, 10000, 15000, 20000, 25000, 30000];
			else if (parseInt(price) <= 600000) frArr = [5000, 7500, 10000, 15000, 20000, 25000, 30000, 40000];
			else
			frArr = [5000, 7500, 10000, 15000, 20000, 25000, 30000, 40000, 50000];
		}
		GutaInfo.frArray = new Array(0);
		for (var i = 1; i <= frArr.length; i++) {
			var frImg = {
				value: null,
				image: null
			}
			if ((parseInt(price) / 100 * i) <= (slider2.maxVal + 1)) {
				try {
					frImg.value = frArr[i - 1];
					frImg.image = $("<img src='red.png' title='Франшиза " + addSpaces(frImg.value.toString()) + " руб. для страховой компании Гута-Страхование'>");
					GutaInfo.frArray.push(frImg);
				} catch (eee) {
					alert('err=' + eee)
				};
			}
		}
		for (var i = 0; i < GutaInfo.frArray.length; i++) {
			var fromLeft = parseInt(GutaInfo.frArray[i].value / slider2.maxVal * slider2.width);
			GutaInfo.frArray[i].image.css("position", "absolute").css("left", fromLeft + "px").css("top", "0px").click(function () {
				$(this).parent().click();
			}).focus(function () {
				$(this).blur()
			}).appendTo($("#barsliderDiv1").eq(0));
			//		GutaInfo.frArray[i].image.css("position","absolute").css("left",fromLeft+"px").css("top","0px").click(function(){$(this).parent().click();}).focus(function(){$(this).blur()}).clone().appendTo($("#barsliderDiv2").eq(0));
		}
	}
	GutaInfo.notesSpecial = 'Особенности расчета страховой компании ГУТА-Страхование:<br>' + GutaInfo.notesSpecial;
	GutaInfo.notesSpecial += "Размер франшизы " + addSpaces(GutaInfo.realFr.toString()) + " руб.";
	GutaInfo.notesToDisplay.push("<span style='margin:0px; padding:0px; margin-left:24px;; position:relative; top:5px;'>Франшиза: " + addSpaces(GutaInfo.realFr.toString()) + " руб.</span><br>");

	//	var time_1= new Date().getTime();
	//	alert(time_1-time_0);
	counterCasco();
}